using System;
using System.Collections.Generic;
using Castle.ActiveRecord;
using DecisionMaking.Domain.Reports;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using NHibernate.Type;

namespace DecisionMaking.Domain
{
    [ActiveRecord]
    public class SimulationOutcome : ActiveRecordBase<SimulationOutcome>
    {
        private IList<AlgorithmExecutionOutcome> _executionOutcomes;

        public SimulationOutcome()
        {
            _executionOutcomes = new List<AlgorithmExecutionOutcome>();
        }

        [PrimaryKey(PrimaryKeyType.GuidComb)]
        public Guid Id { get; set; }

        [BelongsTo(NotNull = true)]
        public Environment Environment { get; set; }

        [BelongsTo(NotNull = true)]
        public AlgorithmDefinition AlgorithmDefinition { get; set; }

        [HasMany(typeof(AlgorithmExecutionOutcome), Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true)]
        public IList<AlgorithmExecutionOutcome> ExecutionOutcomes
        {
            get { return _executionOutcomes; }

            set
            {
                _executionOutcomes = value;
            }
        }

        [Property]
        public DateTime ExecutionDate { get; set; }

        [Property]
        public double AverageRunTime { get; set; }

        [Property]
        public double AverageCuesUsed { get; set; }

        [Property]
        public double AverageCandidatesRecognized { get; set; }

        [Property]
        public double AverageAccuracy { get; set; }

        public void AddExecutionOutcome(AlgorithmExecutionOutcome outcome)
        {
            outcome.Simulation = this;
            _executionOutcomes.Add(outcome);
        }

        public static IList<SimulationOutcome> FindByAccountId(int accountId)
        {
            DetachedCriteria criteria = DetachedCriteria.For(typeof(SimulationOutcome));
            criteria.CreateCriteria("Environment").CreateCriteria("Account").Add(Restrictions.IdEq(accountId));
            criteria.SetFetchMode("ExecutionOutcomes", FetchMode.Lazy);

            return FindAll(criteria);            
        }


    }
}